package com.loooooo.pm2024.utils;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class FontUtils {

    public static void main(String[] args) throws IOException, FontFormatException {
//
//        double DPI = 96 / 25.4;
//        int width = 400;
//        int height = 400;
//        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/fonts/msyh.ttf"));
//        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g = image.createGraphics();
//        g.setColor(Color.BLUE);
//        g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
//        FontRenderContext ctx = g.getFontRenderContext();
//        String text = "设计工具";
//        g.setColor(Color.RED);
//        g.setFont(new Font(font.getName(), Font.BOLD, 0x0FFF));
//
//        //g.drawString(text, 0, 50);
//
//
//        g.getFont().getAvailableAttributes();
//        GlyphVector gv = g.getFont().layoutGlyphVector(ctx, text.toCharArray(), 0, text.toCharArray().length, Font.LAYOUT_LEFT_TO_RIGHT);
//        Shape shape = gv.getOutline();
//
//
//        AffineTransform transform = new AffineTransform();
//        //transform.rotate(Math.PI / 6);
//        transform.translate(100, 100);
//        float sx = (float) (DPI * 5 * 4 / shape.getBounds().getWidth());
//        float sy = (float) (DPI * 10 / shape.getBounds().getHeight());
//        transform.scale(sx, sy);
//        // transform.rotate(Math.PI / 4);
//
//        g.transform(transform);
//        //g.translate(50,100);
//        g.fill(shape);
//        //g.draw(shape);
//        g.dispose();
//        ImageIO.write(image, "png", new File("/temp/2022/11/font2023.png"));

        test2("大家好，。Hello World", 1000, 500);

    }

    public static void test(String text, int width, int height) throws IOException, FontFormatException {
        double DPI = 96 / 25.4;
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/fonts/msyh.ttf"));
        BufferedImage image = new BufferedImage((int) (width * DPI), (int) (height * DPI), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
        FontRenderContext ctx = g.getFontRenderContext();
        //String text = "设计工具";
        g.setColor(Color.RED);
        g.setFont(new Font(font.getName(), Font.BOLD, 0x0FFF));


        Shape shape = getShape(g, text);

        float kc = 1.0F * width / height;
        float kf = (float) (shape.getBounds().getWidth() / shape.getBounds().getHeight());

        int lines = (int) Math.round(Math.sqrt(kf / kc));

        double stringWidth = shape.getBounds().getWidth();
        double stringHeight = shape.getBounds().getHeight();

        double newWidth = stringWidth / lines;
        System.out.println(stringWidth + " " + stringHeight);
        int charStart = 0;
        java.util.List<String> lineString = new ArrayList<>();
        java.util.List<Shape> shapeList = new ArrayList<>();
        for (int ce = charStart + 1; ce <= text.length(); ce++) {
            shape = getShape(g, text.substring(charStart, ce));
            if (shape.getBounds().getWidth() > newWidth) {
                String t = text.substring(charStart, ce - 1);
                lineString.add(t);
                charStart = ce - 1;
                shapeList.add(getShape(g, t));
            }
        }
        if (charStart < text.length()) {
            lineString.add(text.substring(charStart));
            shapeList.add(getShape(g, text.substring(charStart)));
        }


        AffineTransform transform = new AffineTransform();
        //transform.rotate(Math.PI / 6);
        transform.translate(100, 100);
        float sx = (float) (DPI * 5 * 4 / shape.getBounds().getWidth());
        float sy = (float) (DPI * 10 / shape.getBounds().getHeight());
        transform.scale(sx, sy);
        // transform.rotate(Math.PI / 4);

        g.transform(transform);
        float y = 100;
        for (int i = 0; i < shapeList.size(); i++) {
            g.translate(50, y);
            Shape shape1 = shapeList.get(i);
            g.fill(shape1);
            y = (float) shape1.getBounds().getHeight();
        }
        //g.draw(shape);
        g.dispose();
        ImageIO.write(image, "png", new File("/temp/2023/05/font2023.png"));

    }

    public static void test2(String text, int width, int height) throws IOException, FontFormatException {
        double DPI = 96 / 25.4;
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/fonts/msyh.ttf"));
        BufferedImage image = new BufferedImage((int) (width * DPI), (int) (height * DPI), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
        FontRenderContext ctx = g.getFontRenderContext();
        //String text = "设计工具";
        g.setColor(Color.RED);
        g.setFont(new Font(font.getName(), Font.BOLD, 200));

        int offsetX = 20;
        int offsetY = 400;

        Shape shape = getShape(g, text);
        g.drawRect((int)shape.getBounds().getX()+offsetX, (int)shape.getBounds().getY()+offsetY, (int)shape.getBounds().getWidth(),(int)shape.getBounds().getHeight());

//        AffineTransform transform = new AffineTransform();
//        //transform.rotate(Math.PI / 6);
//        transform.translate(100, 100);
//        float sx = (float) (DPI * 5 * 4 / shape.getBounds().getWidth());
//        float sy = (float) (DPI * 10 / shape.getBounds().getHeight());
//        transform.scale(sx, sy);
//        // transform.rotate(Math.PI / 4);
//
//        g.transform(transform);

        g.translate(offsetX, offsetY);

        g.fill(shape);
        //g.draw(shape);
        g.dispose();
        ImageIO.write(image, "png", new File("/temp/2023/05/font2023.png"));

    }

    public static java.util.List<String> wrap(String text, int lines) throws IOException, FontFormatException {
        double DPI = 96 / 25.4;
        int width = 400;
        int height = 200;
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/fonts/msyh.ttf"));
        BufferedImage image = new BufferedImage((int) (width * DPI), (int) (height * DPI), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        FontRenderContext ctx = g.getFontRenderContext();
        g.setFont(new Font(font.getName(), Font.BOLD, 0x0FFF));


        Shape shape = getShape(g, text);

        double stringWidth = shape.getBounds().getWidth();
        double stringHeight = shape.getBounds().getHeight();
        System.out.println(stringWidth + " " + stringHeight);
        double newWidth = stringWidth / lines;
        for (int i = 0; i < text.length(); i++) {
            shape = getShape(g, text.substring(i, i + 1));
            stringWidth = shape.getBounds().getWidth();
            stringHeight = shape.getBounds().getHeight();
            System.out.println(stringWidth + " " + stringHeight);
        }


        AffineTransform transform = new AffineTransform();
        //transform.rotate(Math.PI / 6);
        transform.translate(100, 100);
        float sx = (float) (DPI * 5 * 4 / shape.getBounds().getWidth());
        float sy = (float) (DPI * 10 / shape.getBounds().getHeight());
        transform.scale(sx, sy);
        // transform.rotate(Math.PI / 4);

        g.transform(transform);
        //g.translate(50,100);
        g.fill(shape);
        //g.draw(shape);
        g.dispose();
        ImageIO.write(image, "png", new File("/temp/2022/11/font2023.png"));
        return null;
    }

    public static Shape getShape(Graphics2D g, String text) {
        GlyphVector gv = g.getFont().layoutGlyphVector(g.getFontRenderContext(), text.toCharArray(), 0, text.toCharArray().length, Font.LAYOUT_LEFT_TO_RIGHT);
        return gv.getOutline();
    }

    public static GlyphVector getGlyphVector(Graphics2D g, String text) {
        GlyphVector gv = g.getFont().layoutGlyphVector(g.getFontRenderContext(), text.toCharArray(), 0, text.toCharArray().length, Font.LAYOUT_LEFT_TO_RIGHT);
        return gv;
    }

    private static Shape getShape(Font font, FontRenderContext ctx, String text) {
        GlyphVector gv = font.layoutGlyphVector(ctx, text.toCharArray(), 0, text.toCharArray().length, Font.LAYOUT_LEFT_TO_RIGHT);
        return gv.getOutline();
    }
}
